<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Answers_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    
    function getOne($id)
    {
        $param['field'] = '*';
        
        $this->db->select($param['field']);
        $this->db->from('answers a');
        $this->db->join('answers_metas am', 'am.answers_id=a.answers_id', 'left');
        $this->db->join('metas m', 'am.mid=m.mid', 'left');
        $this->db->join('users u', 'a.answers_uid=u.uid', 'left');
        
        $this->db->where(array('a.answers_id'=>$id));
        $entries = $this->db->get()->row_array();
        //if($isPicture = $this->getPicture($id))
        //    $entries['pictures'] = $isPicture;
        return $entries;
    }
    
    function getAnswers(array $param, $page=0, $limit=20)
    {
        if(!isset($param['field']) || empty($param['field']))
        {
            
            $param['field'] = '*';
        }

        $this->db->select($param['field']);
        $this->db->from('answers a');
        $this->db->join('users u', 'a.answers_uid=u.uid', 'left');
        if(isset($param['where']) && ! empty($param['where']))
        {
            $this->db->where($param['where']);
        }
        if(isset($param['like']) && ! empty($param['like']))
        {
            $this->db->like($param['like']);
        }
        if(!isset($param['order']) || empty($param['order']))
        {
            $param['order']='answers_cdtime desc';
        }
        
        $this->db->order_by($param['order']);
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    
    function add(array $answers)
    {
        $this->load->model('Tags');
        /** 构建插入结构 */
        $insertStruct = array(
            'answers_cdtime'    =>  time(),
            'answers_mdtime'    =>  time(),
            'answers_status'    =>  empty($answers['answers_status']) ? 'publish' : $answers['answers_status'],
            'answers_uid'       =>  isset($answers['uid']) ? $answers['uid'] : $this->session->userdata('uid'),
            'answers_ip'        =>  $answers['answers_ip'],
            'answers_title'     =>  empty($answers['answers_title']) ? NULL : $answers['answers_title'],
            'answers_content'   =>  empty($answers['answers_content']) ? NULL : $answers['answers_content'],
        );

        if (!empty($answers['answers_id']))
        {
            unset($insertStruct['answers_cdtime']);
            $this->db->where('answers_id', $answers['answers_id']);
            $insertId = $this->db->update('answers', $insertStruct);
        }
        else
        {
            /** 首先插入部分数据 */
            $this->db->insert('answers', $insertStruct);
            $insertId = $this->db->insert_id();
        }
        
        /** keyword */
        if($insertId > 0 && !empty($answers['answers_tag']))
        {
            $this->Tags->add_Answers($insertId, $answers['answers_tag']);
        }
        
        return $insertId;
    }
    
    
    public function deleteAnswers($answers_id)
    {
        //删除tag关联
        $this->load->model('Tags');
        $this->Tags->deleteAnswers($answers_id);
                
        //删除Answers
        return $this->db->delete('answers', array('answers_id' => $answers_id));
    }
}